import { Button } from "antd";
import React, { useReducer } from "react";

const initReducer = (state: any, action: any) => {
  switch (action.type) {
    case "+":
      return { count: state.count + 1 };
    case "-":
      return { count: state.count - 1 };
    default:
      return state;
  }
};

const TestComponent: React.FC = () => {
  // 测试的状态变量
  const [state, dispatch] = useReducer(initReducer, { count: 0 });
  console.log("组件更新了");

  //
  const [count, setCount] = React.useState(0);
  return (
    <div>
      <h1>This is a test component</h1>
      <Button onClick={() => dispatch({ type: "+" })}>+</Button>{" "}
      <span>{state.count}</span>{" "}
      <Button onClick={() => dispatch({ type: "-" })}>-</Button>
      <div>
        <span>count: {count}</span>
        <Button onClick={() => setCount(count + 1)}>+</Button>
      </div>
    </div>
  );
};

export default TestComponent;
